package com.zzptc.my.myrecipe;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.zzptc.my.myrecipe.Adapter.MyRvAdapter;
import com.zzptc.my.myrecipe.Service.MyAppService;
import com.zzptc.my.myrecipe.databinding.ActivityMainBinding;
import com.zzptc.my.myrecipe.entity.RecipeModel;
import com.zzptc.my.myrecipe.entity.ResultsBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    ActivityMainBinding binding;
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView();

      binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
       // RecyclerView rv = findViewById(R.id.rv);
       // binding.rv
        getAllRecipe();
    }

    private void getAllRecipe() {
        MyAppService myAppService = retrofit.create(MyAppService.class);
        Call<RecipeModel> allRecipte = myAppService.getAllRecipte(10);
        allRecipte.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
             List<ResultsBean> re = response.body().getResults();
                for (ResultsBean result:re) {
                    result.save();
                }
                MyRvAdapter adapter=new MyRvAdapter(MainActivity.this);//传上下文
                binding.rv.setAdapter(adapter);
               // Log.d(TAG, "224055: "+re.getResults().size());
              //  Log.d(TAG, "onResponse: "+new Gson().toJson(response.body()));//打印全部数据

            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getMessage());
            }
        });
    }
}
